<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	<title>众心起盒团膳</title>
	<link rel="stylesheet" href="css/weui.min.css" />
	<link rel="stylesheet" href="fonts/iconfont.css" />
	<link rel="stylesheet" href="./fonts/qiheicon.css" />
	<link rel="stylesheet" href="css/style.css" />
	<link rel="stylesheet" href="css/home.css" />
	<script src="js/jquery-2.2.3.min.js"></script>
	<!-- <script src="js/weui.min.js"></script> -->
</head>
<style>
	.weui-loadmore {
		width: 100%;
		height: 100%;
		font-size: 14px;
		text-align: center;
		position: absolute;
		background: #fff;
		opacity: 1;
		z-index: 999;
		color: #fff;
		margin: 0px;
		line-height: 400px;
	}

	.weui-loading {
		width: 40px;
		height: 40px;
	}
</style>

<body ontouchstart>
	<div class="weui-content">
		<div class="loading" style="display:none;">
			<div class="weui-loadmore">
				<i class="weui-loading"></i>
			</div>
		</div>
		<!-- 提示token过期 -->
		<div class="weui-dialog" style="display:none;">
			<div class="weui-dialog__bd">登录失效，请重新登录</div>
			<div class="weui-dialog__ft">
				<a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary">知道了</a>
			</div>
		</div>
		<div class="weui-flex user-head">
			<div class="touxiang">
				<!-- <img id="wxImg" src="" style="width:70px; height:70px; border-radius: 0;"> -->
			</div>
			<div class="weui-flex__item">
				<div><span class="name" id="userName"></span></div>
				<div class="info" id="phone"></div>
				<div id="companyBox" style="font-size:12px;">
					<div class="info" id="company"></div>
				</div>
			</div>
		</div>
		<div class="weui-cells nav-order">
			<div class="weui-cell weui-cell_access">
				<div class="weui-cell__bd">我的订单</div>
				<!-- orderAll.html -->
				<a href="#" class="weui-cell__ft weui-cell__ft_in-access more">查看全部</a>
			</div>
			<!-- pendingPayment.html -->
			<div class="weui-cell weui-flex">
				<a href="#" class="weui-flex__item">
					<!-- <div class="iconfont icon-dfk"></div> -->
					<span class="iconqihe-dingdandaifukuan" style="font-size:26px; color:#F94866"></span>
					<div class="txt">
						待付款
					</div>
					<!-- <span class="weui-badge">9</span> -->
				</a>
				<!-- pendingDelivery.html -->
				<a href="#" class="weui-flex__item">
					<!-- <img src="./images/dingdandaifahuo.png"> -->
					<span class="iconqihe-dingdandaifahuo" style="font-size:22px; color:#F94866"></span>
					<div class="txt">待发货</div>
				</a>
				<!-- goodsReceive.html -->
				<a href="#" class="weui-flex__item">
					<!-- <img src="./images/dingdandaishouhuo.png"> -->
					<span class="iconqihe-dingdandaishouhuo" style="font-size:22px; color:#F94866"></span>
					<div class="txt">待收货</div>
				</a>
				<!-- goodsCompleted.html -->
				<a href="#" class="weui-flex__item">
					<!-- <img src="./images/dingdandaipingjia.png"> -->
					<span class="iconqihe-dingdandaipingjia" style="font-size:26px; color:#F94866"></span>
					<div class="txt">待评价</div>
				</a>
			</div>

		</div>
		<div class="weui-cells nav-user">
			<!-- userinfo.html 个人资料 -->
			<a href="userinfo.html" class="weui-cell weui-cell_access">
				<div class="weui-cell__hd"><i class="iconfont icon-user"></i></div>
				<div class="weui-cell__bd">个人资料</div>
				<div class="weui-cell__ft"></div>
			</a>
			<!-- zhanghu.html 财务中心 -->
			<a href="zhanghu.html" class="weui-cell weui-cell_access">
				<div class="weui-cell__hd"><i class="iconfont icon-gold"></i></div>
				<div class="weui-cell__bd">财务中心</div>
				<div class="weui-cell__ft"></div>
			</a>
			<!-- fukuanma.html 付款码 -->
			<a href="fukuanma.html" class="weui-cell weui-cell_access">
				<div class="weui-cell__hd"><i class="iconfont icon-qrcode"></i></div>
				<div class="weui-cell__bd">我的付款码</div>
				<div class="weui-cell__ft"></div>
			</a>
			<!-- fukuanma.html我的优惠券 -->
			<a href="#" class="weui-cell weui-cell_access">
				<div class="weui-cell__hd">
					<img src="./images/youhuiquan.png" class="youhuiquan">
				</div>
				<div class="weui-cell__bd">我的优惠券</div>
				<div class="weui-cell__ft"></div>
			</a>
		</div>
	</div>
	<!-- 点击加入公司的弹窗 -->
	<div class="popup">
		<div class="popup-content-bg"></div>
		<div class="popup-content">
			<img src="./images/weijiarugongs_tubiao@2x.png" alt="">
			<div id="joinText">
				请联系管理员加入公司
			</div>
			<div id="closePopup">
				知道了
			</div>
		</div>
	</div>
</body>
<script src="./api.js"></script>
<script>
	window.onload = function () {
		let api = getApi();
		//判断是否有用户信息缓存，无则跳转至登录页面
		if (localStorage.getItem('wxUserInfo')) {
			//获取本地缓存的用户信息
			let userinfo = JSON.parse(localStorage.getItem('wxUserInfo')).data
			let customerID = userinfo.customer.customerID;
			let token = userinfo.token;
			console.log(userinfo, customerID, token)

			//显示公司名称
			userinfo.customer.enterpriseName == null
				? $('#company').html('公司名称：  <a href="#" class="blur-color popupsBtn" style="color: #fff; border: 1px solid #fff; border-radius: 4px; padding: 2px 4px;">   未加入</a>')
				: $('#companyBox').text(userinfo.customer.enterpriseName)

			//点击加入公司时弹窗
			$('#company').on('click', function () {
				$('.popup').css('display', 'block')
				$('#closePopup').click(function () {
					$('.popup').css('display', 'none')
				})
				$('.popup-content-bg').click(function () {
					$('.popup').css('display', 'none')
				})
			})

			$('.loading').css('display', 'block');

			//=========获取用户头像===========
			fetch(`${api}/getWxUserInfo?customerID=${customerID}`, {
				method: "get",
				headers: {
					'Content-type': 'application/json',
					"Authorization": token
				}
			})
				.then(res => {
					if (res.status == 401) {
						alert('登录失效，请重新登录！')
						location.href = './index.html';
						return;
					}
					return res;
				})
				.then(res => res.json())
				.then(data => {
					let getWxImg = JSON.parse(data.data).headimgurl

					//缓存头像
					localStorage.setItem('getWxImg', getWxImg)
					// 显示微信头像
					$('.touxiang').html(`<img id="wxImg" src="${getWxImg}" style="width:70px; height:70px; border-radius: 0;">`)
					$('.loading').css('display', 'none');
				})
				.catch(err => {
					console.log(err)
				})
			//获取用户姓名和电话号码
			fetch(`${api}/customer/${customerID}`, {
				method: "get",
				headers: {
					'Content-type': 'application/json',
					"Authorization": token
				}
			})
				.then(res => res.json())
				.then(data => {
					console.log(data)
					// 姓名
					data.data.customerName ? $('#userName').text(data.data.customerName) : '--'
					// 电话号码
					data.data.phoneNumber ? $('#phone').text(data.data.phoneNumber) : '--'
				})

		} else {
			location.href = './index.html'
		}
	}
</script>

</html>